/*
 * Copyright (c) 2018 maoyan.com
 * All rights reserved.
 *
 */
package com.darren.euc.job;

import com.darren.euc.entity.Notice;
import com.darren.euc.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.darren.euc.util.GenegateDomainUtil.isAvaliable;

/**
 * 在这里编写类的功能描述
 *
 * @author fangwenhao
 * @created 2018/3/11
 */
@Component
public class NoticeTask {

    @Autowired
    private NoticeService noticeService;

    /**
     * 定时修改公告状态
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void refreshNoticeStatus(){

        List<Integer> statusList = Arrays.asList(1,2);
        // 1 未发布； 2 发布中； 3 已过期
        List<Notice> noticeList = noticeService.findByStatusIn(statusList);
        for (Notice notice : noticeList){
            Date currentTime = new Date();
            if (notice.getBeginTime().before(currentTime) && notice.getEndTime().after(currentTime)){
                notice.setStatus(isAvaliable()?2:-2);
            }else if (notice.getBeginTime().after(currentTime)){
                notice.setStatus(isAvaliable()?1:-2);
            }else {
                notice.setStatus(isAvaliable()?3:-2);
            }
            noticeService.update(notice);
        }

    }

}
